const fs = require('fs')
const path = require('path')
const mysql = require('mysql');
module.exports = {
    // 写入文件封装函数
    writeFile(url, data) {
        return new Promise((reslove, reject) => {
            fs.writeFile(path.join(__dirname, '../../' + url), data, (err) => {
                if (err) {
                    throw reject(err)
                }
                reslove(true)
            })
        })
    },
    // 读取文件封装函数
    readFile(url) {
        return new Promise((resolve, reject) => {
            fs.readFile(path.join(__dirname, '../../' + url), (err, data) => {
                if (err) {
                    throw reject(false)
                }
                return resolve(data.toString())
            })
        })
    },
    // 日期格式化处理函数
    dateFormat() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        return year + '年' + month + '月' + day + '日 '
    },
    // mysql查询数据库处理函数
    queryDatabase(sql, addSqlParams = '') {
        return new Promise((resolve, reject) => {
            let connection = mysql.createConnection({
                host: 'localhost',
                user: 'root',
                password: 'cj1008611',
                database: 'bodhi-netdisk'
            });
            connection.connect();
            connection.query(sql, addSqlParams, (error, results, fields) => {

                if (error) {
                    return reject('后台数据库走丢了,请稍后重试！');
                }
                return resolve(results)
            });
            connection.end();
        })
    },

    createSixNum() {
        var Num = "";
        for (var i = 0; i < 6; i++) {
            Num += Math.floor(Math.random() * 10);
        }
        return Num;
    }

}